package com.lms.demo.fileservice.aspect;

import com.lms.demo.fileservice.annotation.Log;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;

/**
 * @Author: 李孟帅
 * @CreateTime: 2020-12-24 16:35
 * @Description:
 */
@Aspect
@Component
public class LogAspect {
    private static final String LINE = "==========================================》{}";
    private static final Logger LOGGER = LoggerFactory.getLogger(LogAspect.class);
    @Pointcut("@annotation(com.lms.demo.fileservice.annotation.Log)")
    public void logPointCut() {
    }

    @AfterReturning("logPointCut()")
    public void afterReturning(JoinPoint joinPoint) {
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        Method method = signature.getMethod();
        Log annotation = method.getAnnotation(Log.class);
        if (annotation != null) {
            String value = annotation.value();
            LOGGER.info( value + LINE, "成功");
        }
    }


}
